package net.toocruel.iqismart.web.controller.core;

import net.toocruel.iqismart.dao.core.ArticleRepository;
import net.toocruel.iqismart.dao.core.AttentionRepository;
import net.toocruel.iqismart.dao.core.CollectionRepository;
import net.toocruel.iqismart.entity.constants.SourceType;
import net.toocruel.iqismart.entity.core.Collection;
import net.toocruel.iqismart.service.UserService;
import net.toocruel.iqismart.service.core.CategoryService;
import net.toocruel.iqismart.service.core.CommentService;
import net.toocruel.iqismart.service.core.QuestionService;
import net.toocruel.iqismart.service.core.TagService;
import net.toocruel.iqismart.web.controller.base.AbstractBaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.criteria.Predicate;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("collect")
public class CollectController extends AbstractBaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private TagService tagService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private AttentionRepository attentionRepository;

    @Autowired
    private ArticleRepository articleRepository;

    @Autowired
    private CollectionRepository collectionRepository;
    @GetMapping("{sourceType}/{sourceId}")
    @ResponseBody
    public String collect(@PathVariable SourceType sourceType,
                               @PathVariable Long sourceId ,
                               HttpServletRequest request){

        Collection one = collectionRepository.findOne((root, query, cb) -> {
            Predicate user = cb.equal(root.get("user"), getCurrentUser());
            Predicate source = null;
            switch (sourceType) {
                case question:
                    source = cb.equal(root.get("question"), questionService.findById(sourceId));
                    break;

                case article:
                    source = cb.equal(root.get("article"), articleRepository.findOne(sourceId));
                    break;

            }
            return cb.and(user, source);
        });

        if(one!=null){
            collectionRepository.delete(one);
            return "uncollected";
        }

        Collection collection = new Collection();
        collection.setSourceType(sourceType);
        switch (sourceType){
            case question:
                collection.setQuestion(questionService.findById(sourceId));
                break;

            case article:
                collection.setArticle(articleRepository.findOne(sourceId));
                break;

        }
        collection.setUser(getCurrentUser());

        collectionRepository.save(collection);

        return "collected";
    }

    @Override
    protected String getModule() {
        return "follow";
    }
}
